Loops.
Published on: Mon Feb 15 2010
Iterative Programming Repeat same task over and over w/ different data
Loop Elements
1. Initialization
2. Test loop condition
3. Loop Body (Task to repeat)
4. Loop Update.
3 Types of Loop
- For: Loop update in header, fixed number of iterations, pretest.
- While: Loop update in body, execute 0 or more. Pretest
- Do…. while: execute at least once, post test.
for ( initialization; loop condition test; loop update)
{
loop body;
}
Fancy Loop header
for (i=0,j=1; i < 5; i++, j++)
Loop headers can include calls to functions and omit headers.
while (loop condition)
{
loop body
loop update
}